package com.example.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 *	RequestHand工具类，用于存取登录用户session
 */
public class UserContext {

	private static final String USER_IN_SESSION = "CURRENT_USER";

	/**
	 * 获取session
	 */
	public static HttpSession getSession() {
		return ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes())).getRequest().getSession();
	}


	/**
	 * 将用户存入session中
	 */
	public static void setCurrentUser(Object obj) {
		
		if(obj == null) {
			getSession().invalidate();
		}else {
			getSession().setAttribute(USER_IN_SESSION,obj);
		}
	}

	/**
	 * 获取session中存的用户
	 */
	public static Object getCurrentUser() {
		return (Object) getSession().getAttribute(USER_IN_SESSION);
	}
	
}
